import { Injectable } from '@angular/core';
declare var require:any;
var giz = require('../../assets/js/gizwits');

@Injectable()
export class NotiService {
  private gizwitsNotiWs;
  private enterpriseId:string = '96d09b9d8d8c4086bca6b0682c60e353';
  private enterpriseSecret:string = '6e4fa5d6081e4ce4b0fa3d5caf5f5c73';
  private host:string = 'noti.gizwits.com';
  private port:string = '2016';



  init(pk,macs,eventType,onEvent,that = null){

    this.gizwitsNotiWs = new giz.GizwitsNotiWS(this.enterpriseId, this.enterpriseSecret, this.host, this.port, that);
    this.gizwitsNotiWs.onConnected = this.onConnected(pk,macs,eventType);
    this.gizwitsNotiWs.onSubscribed = this.onSubscribed;
    this.gizwitsNotiWs.onEvent = onEvent;
    this.gizwitsNotiWs.onError = this.onError;
    return this.gizwitsNotiWs;
    //gizwitsNotiWs.connect();
  }

  onConnected(pk,macs,eventType){
    console.log("完成onConnected!可以开始订阅事件。");

    let data = [{
      "product_key": pk,
      "macs": macs,
      "events": eventType
    }];
    if (data.length > 0) {
      console.log("开始订阅事件!");
      this.gizwitsNotiWs.subscribe(data);
    }
  }

  onSubscribed(){
    console.log("完成onSubscribed!可以开始接受消息。");
  }

  onError(value){
    console.log(value.toString());
  }
}
